package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.TermInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.TermInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.TermInfo;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TermInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntTermInfoUpdateRequest;
import cc.rengu.igas.bomp.facade.response.MchntTermInfoUpdateResponse;
import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;

/**
 * 商户终端信息更新
 * Created by wangcheng on 2021/10/22 16:20
 */
public class MchntTermInfoUpdateService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntTermInfoUpdateRequest mchntTermInfoUpdateRequest = new MchntTermInfoUpdateRequest();
        ConvertUtil.convertOutput(mchntTermInfoUpdateRequest);
        return mchntTermInfoUpdateRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntTermInfoUpdateRequest mchntTermInfoUpdateRequest = (MchntTermInfoUpdateRequest) request;
        String oprType = mchntTermInfoUpdateRequest.getOprType();
        TermInfoBean termInfoBean = mchntTermInfoUpdateRequest.getTermInfo();
        if (StringUtil.isEmptyOrNull(oprType) || null == termInfoBean) {
            rglog.error("更新类型或者终端信息为空,oprType:<{}>,termInfo:<{}>", oprType, JSON.toJSONString(termInfoBean));
            throw new BizException(RespCodeEnum.INVALID_REQEST_PARAM.getRespCode(), RespCodeEnum.INVALID_REQEST_PARAM.getRespDesc());
        }
        MchntTermInfoUpdateResponse mchntTermInfoUpdateResponse = new MchntTermInfoUpdateResponse();
        mchntTermInfoUpdateResponse.setHeader(mchntTermInfoUpdateRequest.getHeader());
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(mchntTermInfoUpdateResponse);

        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(termInfoBean.getInstId());
        termInfo.setMchntNo(termInfoBean.getMchntNo());
        termInfo.setTermNo(termInfoBean.getTermNo());
        termInfo.setTermType(termInfoBean.getTermType());
        termInfo.setPosInstCd(termInfoBean.getAcqInstId());
        termInfo.setTermAddrArea(termInfoBean.getTermAreaCode());
        termInfo.setLongitude(termInfoBean.getLongitude());
        termInfo.setLatitude(termInfoBean.getLatitude());
        termInfo.setEffectiveRadius(termInfoBean.getEffectiveRadius());
        termInfo.setSn(termInfoBean.getTermSn());
        termInfo.setIcDownSign(termInfoBean.getIcParamDownFlag());
        termInfo.setKeyDownSign(termInfoBean.getIcKeyDownFlag());
        termInfo.setTermSignStatus(termInfoBean.getTermSignStatus());
        termInfo.setAutoSignoutFlag(termInfoBean.getAutoSignOutFlag());
        termInfo.setBathcSettleNum(termInfoBean.getBatchNo());
        termInfo.setTermStatus(termInfoBean.getTermStatus());
        termInfo.setContactName(termInfoBean.getContactName());
        termInfo.setContactPhone(termInfoBean.getContactPhone());
        TermInfoMapper termInfoMapper = new TermInfoMapperImpl();
        int iReturnCode = termInfoMapper.updateBompTermInfoByPrimaryKey(termInfo, mchntTermInfoUpdateRequest.getOprType());
        if (0 != iReturnCode) {
            rglog.error("更新终端状态失败,instId:<{}>,mchntNo:<{}>,termNo:<{}>", termInfo.getInstId(), termInfo.getMchntNo(), termInfo.getTermNo());
            bizResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        } else {
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntTermInfoUpdateResponse mchntTermInfoUpdateResponse = (MchntTermInfoUpdateResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntTermInfoUpdateResponse);
    }
}
